Skip to content

Properly re-own type variables when merging constraints #12519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2021

Conversation

smarter
Copy link
Member

@smarter smarter commented May 18, 2021

The documentation for myOwnedVars used to say that it could contain
already instantiated variables because "weak references can have
spurious nulls" but I think that's incorrect: the documentation of
WeakReference makes no mention of spurious nulls, the object it
references should only be replaced by null if it cannot be reached
through a non-weak reference. Instead I believe the issue was that
mergeConstraintWith did not set the owningState of type variables
that it now owns.

@smarter smarter marked this pull request as draft May 18, 2021 17:04
@smarter smarter marked this pull request as ready for review May 19, 2021 13:05
@smarter smarter requested a review from odersky May 19, 2021 13:05
The documentation for myOwnedVars used to say that it could contain
already instantiated variables because "weak references can have
spurious nulls" but I think that's incorrect: the documentation of
WeakReference makes no mention of spurious nulls, the object it
references should only be replaced by null if it cannot be reached
through a non-weak reference. Instead I believe the issue was that
`mergeConstraintWith` did not set the `owningState` of type variables
that it now owns.
@@ -161,8 +163,7 @@ class TyperState() {
constraint.typeVarOfParam(tl.paramRefs(0)) ne other.typeVarOfParam(tl.paramRefs(0))
// Note: Since TypeVars are allocated in bulk for each type lambda, we only
// have to check the first one to find out if some of them are different.
val conflicting = constraint.domainLambdas.find(tl =>
other.contains(tl) && hasConflictingTypeVarsFor(tl))
val conflicting = constraint.domainLambdas.find(constraint.hasConflictingTypeVarsFor(_, other))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find not filter? It was like this before, but the logic seems to require a filter. Or am I overlooking something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I didn't notice, I'll fix it!

@odersky odersky assigned smarter and unassigned odersky May 26, 2021
smarter added 2 commits May 26, 2021 17:18
After the previous commit a few tests started failing due to TyperStates
attempting to instantiate a type variable with an owningState pointing
to a different TyperState. The issue is that after
`mergeConstraintWith`, multiple TyperState can own the same type
variable. This is fine as long as only one of them is committable since
the other ones won't attempt to instantiate any type variable, but
that's not necessarily the case in FunProto#typedArgs where we merge the
constraints of the FunProto context into the passed context.

This commit fixes this by instantiating any type variable in the
constraints of the FunProto which do not exist in the passed TyperState
before calling `mergeConstraintWith`. This ensures that merging can be
done without changing the ownership of any type variable, thus keeping
both TyperState safely committable.
The use of find instead of filter appears to be a typo.
@smarter smarter enabled auto-merge May 26, 2021 15:20
@smarter smarter merged commit 7599095 into scala:master May 26, 2021
@smarter smarter deleted the reown-tvars branch May 26, 2021 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants